package com.yuexunit.mobilenurse.module.NursingDocuments.ui;

import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.yuexunit.mobilenurse.R;
import com.yuexunit.mobilenurse.config.AppConfig;
import com.yuexunit.mobilenurse.module.NursingDocuments.model.impl.DocumentRecordDetailModel;
import com.yuexunit.mobilenurse.module.NursingDocuments.presenter.impl.DocumentRecordDetailPresenter;
import com.yuexunit.mobilenurse.module.NursingDocuments.ui.view.IDocumentRecordDetailView;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/12/29.
 */
public class Act_DocumentAcromegalicDetail extends KJActivity implements IDocumentRecordDetailView {

    @Bind(R.id.signinput_name)
    TextView signinputName;
    @Bind(R.id.signinput_bedno)
    TextView signinputBedno;
    @Bind(R.id.signinput_visitno)
    TextView signinputVisitno;
    @Bind(R.id.documentacic_temp_edt)
    EditText documentacicTempEdt;
    @Bind(R.id.type_name)
    TextView typeName;
    @Bind(R.id.documentacic_bp_01)
    EditText documentacicBp01;
    @Bind(R.id.documentacic_bp_02)
    EditText documentacicBp02;
    @Bind(R.id.documentacic_pulse_edit)
    EditText documentacicPulseEdit;
    @Bind(R.id.documentacic_heart_edit)
    EditText documentacicHeartEdit;
    @Bind(R.id.documentacic_breathe_edit)
    EditText documentacicBreatheEdit;
    @Bind(R.id.documentacic_bos_edit)
    EditText documentacicBosEdit;
    @Bind(R.id.documentacic_pupilleft_edit)
    EditText documentacicPupilleftEdit;
    @Bind(R.id.documentacic_pupilright_edit)
    EditText documentacicPupilrightEdit;
    @Bind(R.id.documentacic_lightleft_edit)
    Spinner documentacicLightleftEdit;
    @Bind(R.id.documentacic_lightright_edit)
    Spinner documentacicLightrightEdit;
    @Bind(R.id.documentacic_body_upright_edit)
    Spinner documentacicBodyUprightEdit;
    @Bind(R.id.documentacic_body_upleft_edit)
    Spinner documentacicBodyUpleftEdit;
    @Bind(R.id.documentacic_body_downright_edit)
    Spinner documentacicBodyDownrightEdit;
    @Bind(R.id.documentacic_body_downleft_edit)
    Spinner documentacicBodyDownleftEdit;
    @Bind(R.id.documentacic_aware_edit)
    Spinner documentacicAwareEdit;
    @Bind(R.id.documentacic_health_edit)
    Spinner documentacicHealthEdit;
    @Bind(R.id.documentacic_healthcustom_edit)
    EditText documentacicHealthcustomEdit;
    @Bind(R.id.documentacic_nursed_edit)
    Spinner documentacicNursedEdit;
    @Bind(R.id.documentacic_nursed_f_edit)
    EditText documentacicNursedFEdit;
    @Bind(R.id.documentacic_nursed_g_edit)
    EditText documentacicNursedGEdit;
    @Bind(R.id.documentacic_nursed_h_edit)
    EditText documentacicNursedHEdit;

    public String inpNo;//住院号
    public String recorder;//记录人员id

    private DocumentRecordDetailPresenter presenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_documentacromegalicdetail);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new DocumentRecordDetailPresenter(new DocumentRecordDetailModel(), this);
        inpNo = PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_VISITNO);
        recorder = PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGID);
    }

    @Override
    public void initWidget() {
        super.initWidget();
        signinputName.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_NAME));
        signinputBedno.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_BEDCODE));
        signinputVisitno.setText(inpNo);
    }

    @OnClick({R.id.titlebar_img_back, R.id.right_text, R.id.titlebar_img_menu})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_img_back:
                finish();
                break;
            case R.id.right_text:
                buildValue();
                break;
            case R.id.titlebar_img_menu:
                buildValue();
                break;
        }
    }

    private void buildValue() {
        StringBuilder builder = new StringBuilder();
        if (!StringUtils.isEmpty(documentacicTempEdt.getText().toString())) {
            builder.append("738" + ":" + documentacicTempEdt.getText().toString());
        }

        if (!(StringUtils.isEmpty(documentacicBp01.getText()) && StringUtils.isEmpty(documentacicBp02.getText()))) {
            if (builder.length() != 0) {
                builder.append(",775:" + documentacicBp01.getText() + "/" + documentacicBp02.getText());
            } else {
                builder.append("775:" + documentacicBp01.getText() + "/" + documentacicBp02.getText());
            }
        }

        if (!StringUtils.isEmpty(documentacicPulseEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",777:" + documentacicPulseEdit.getText() );
            } else {
                builder.append("777:" + documentacicPulseEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentacicHeartEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",782:" + documentacicHeartEdit.getText() );
            } else {
                builder.append("782:" + documentacicHeartEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentacicBreatheEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",783:" + documentacicBreatheEdit.getText() );
            } else {
                builder.append("783:" + documentacicBreatheEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentacicBosEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",784:" + documentacicBosEdit.getText() );
            } else {
                builder.append("784:" + documentacicBosEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentacicPupilleftEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",788:" + documentacicPupilleftEdit.getText() );
            } else {
                builder.append("788:" + documentacicPupilleftEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentacicPupilrightEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",794:" + documentacicPupilrightEdit.getText() );
            } else {
                builder.append("794:" + documentacicPupilrightEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentacicLightleftEdit.getSelectedItem().toString())) {
            if (builder.length() != 0) {
                builder.append(",802:" + documentacicLightleftEdit.getSelectedItem().toString().substring(0, 1));
            } else {
                builder.append("802:" + documentacicLightleftEdit.getSelectedItem().toString().substring(0, 1));
            }
        }

        if (!StringUtils.isEmpty(documentacicLightrightEdit.getSelectedItem().toString())) {
            if (builder.length() != 0) {
                builder.append(",804:" + documentacicLightrightEdit.getSelectedItem().toString().substring(0, 1));
            } else {
                builder.append("804:" + documentacicLightrightEdit.getSelectedItem().toString().substring(0,1));
            }
        }

        if (!StringUtils.isEmpty(documentacicBodyUprightEdit.getSelectedItem().toString())) {
            if (builder.length() != 0) {
                builder.append(",819:" + documentacicBodyUprightEdit.getSelectedItem().toString().substring(0, 1));
            } else {
                builder.append("819:" + documentacicBodyUprightEdit.getSelectedItem().toString().substring(0,1));
            }
        }

        if (!StringUtils.isEmpty(documentacicBodyUpleftEdit.getSelectedItem().toString())) {
            if (builder.length() != 0) {
                builder.append(",813:" + documentacicBodyUpleftEdit.getSelectedItem().toString().substring(0, 1));
            } else {
                builder.append("813:" + documentacicBodyUpleftEdit.getSelectedItem().toString().substring(0,1));
            }
        }

        if (!StringUtils.isEmpty(documentacicBodyDownrightEdit.getSelectedItem().toString())) {
            if (builder.length() != 0) {
                builder.append(",822:" + documentacicBodyDownrightEdit.getSelectedItem().toString().substring(0, 1));
            } else {
                builder.append("822:" + documentacicBodyDownrightEdit.getSelectedItem().toString().substring(0,1));
            }
        }

        if (!StringUtils.isEmpty(documentacicBodyDownleftEdit.getSelectedItem().toString())) {
            if (builder.length() != 0) {
                builder.append(",821:" + documentacicBodyDownleftEdit.getSelectedItem().toString().substring(0, 1));
            } else {
                builder.append("821:" + documentacicBodyDownleftEdit.getSelectedItem().toString().substring(0,1));
            }
        }

        if (!StringUtils.isEmpty(documentacicAwareEdit.getSelectedItem().toString())) {
            if (builder.length() != 0) {
                builder.append(",823:" + documentacicAwareEdit.getSelectedItem().toString().substring(0, 1));
            } else {
                builder.append("823:" + documentacicAwareEdit.getSelectedItem().toString().substring(0,1));
            }
        }

        if (!StringUtils.isEmpty(documentacicHealthEdit.getSelectedItem().toString())) {
            if (builder.length() != 0) {
                if(documentacicHealthEdit.getSelectedItem().toString().substring(0,1).equals("K"))
                {
                    builder.append(",824:" + documentacicHealthEdit.getSelectedItem().toString().substring(0, 1));
                    builder.append(",832:" + documentacicHealthcustomEdit.getText());
                }
                else {
                    builder.append(",824:" + documentacicHealthEdit.getSelectedItem().toString().substring(0, 1));
                }
            } else {
                if(documentacicHealthEdit.getSelectedItem().toString().substring(0,1).equals("Z"))
                {
                    builder.append("824:" + documentacicHealthEdit.getSelectedItem().toString().substring(0,1));
                    builder.append(",832:" + documentacicHealthcustomEdit.getText());
                }
                else {
                    builder.append("824:" + documentacicHealthEdit.getSelectedItem().toString().substring(0,1));
                }
            }
        }

        if (!StringUtils.isEmpty(documentacicNursedEdit.getSelectedItem().toString())) {
            if (builder.length() != 0) {
                if(documentacicNursedEdit.getSelectedItem().toString().substring(0,1).equals("F"))
                {
                    builder.append(",825:" + documentacicNursedEdit.getSelectedItem().toString().substring(0, 1));
                    builder.append(",833:" + documentacicNursedFEdit.getText());
                }
                else if(documentacicNursedEdit.getSelectedItem().toString().substring(0,1).equals("G"))
                {
                    builder.append(",825:" + documentacicNursedEdit.getSelectedItem().toString().substring(0, 1));
                    builder.append(",834:" + documentacicNursedGEdit.getText());
                }
                else if(documentacicNursedEdit.getSelectedItem().toString().substring(0,1).equals("H"))
                {
                    builder.append(",825:" + documentacicNursedEdit.getSelectedItem().toString().substring(0, 1));
                    builder.append(",835:" + documentacicNursedHEdit.getText());
                }
                else {
                    builder.append(",825:" + documentacicNursedEdit.getSelectedItem().toString().substring(0, 1));
                }
            } else {
                if(documentacicNursedEdit.getSelectedItem().toString().substring(0,1).equals("F"))
                {
                    builder.append("825:" + documentacicNursedEdit.getSelectedItem().toString().substring(0, 1));
                    builder.append(",833:" + documentacicNursedFEdit.getText());
                }
                else if(documentacicNursedEdit.getSelectedItem().toString().substring(0,1).equals("G"))
                {
                    builder.append("825:" + documentacicNursedEdit.getSelectedItem().toString().substring(0, 1));
                    builder.append(",834:" + documentacicNursedGEdit.getText());
                }
                else if(documentacicNursedEdit.getSelectedItem().toString().substring(0,1).equals("H"))
                {
                    builder.append("825:" + documentacicNursedEdit.getSelectedItem().toString().substring(0, 1));
                    builder.append(",835:" + documentacicNursedHEdit.getText());
                }
                else {
                    builder.append("825:" + documentacicNursedEdit.getSelectedItem().toString().substring(0, 1));
                }
            }
        }


        Map<String, String> params = new HashMap<>();
        params.put("inpNo", inpNo);
        params.put("timestamp", System.currentTimeMillis() + "");
        params.put("timepoint", System.currentTimeMillis() + "");
        params.put("templateId", "37");
        params.put("signData", builder.toString());
        params.put("recorder", recorder);
        presenter.uploadRecord(params);
    }

    @Override
    public void uploadRecord() {
        finish();
    }
}
